{[{ define "admin/editblog.html" }]}
<!DOCTYPE html>
<html lang="en">
<head>
    {[{template "admin/headerbody" .}]}

    <link rel="stylesheet" href="../../static/editor.md/css/editormd.min.css"/>
</head>
<body>
<div id="app">

    {[{template "admin/header" .}]}

    <div class="container grid-lg indexcontent" style="padding-bottom: 40px">

        <div class="columns">
            <div class="column col-12 col-md-12 col-sm-12  col-xs-12">

                <form class="form-horizontal">
                    <div class="form-group">
                        <div class="col-3 col-sm-12">
                            <label class="form-label" for="input-title">标题</label>
                        </div>
                        <div class="col-9 col-sm-12">
                            <input class="form-input" type="text" id="input-title" v-model="blog.title">
                        </div>
                    </div>

                    <div class="form-group">

                        <label class="form-label" for="input-urltitle">标题网址(生于在url中访问，比如
                            http://www.baidu.com/my-computer-is-beautiful)</label>

                        <input class="form-input" type="text" id="input-urltitle" placeholder="比如:my-first-blog"
                               v-model="blog.urltitle">

                    </div>

                    <div class="form-group">
                        <div class="col-3 col-sm-12">
                            <label class="form-label" for="input-istop">是否置顶</label>
                        </div>
                        <div class="col-9 col-sm-12">
                            <label class="form-checkbox">
                                <input type="checkbox" id="input-istop" v-model="blog.istop">
                                <i class="form-icon"></i>
                            </label>

                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-3 col-sm-12">
                            <label class="form-label" for="input-tags">标签(多个以单引号隔开)</label>
                        </div>
                        <div class="col-9 col-sm-12">
                            <input class="form-input" type="text" id="input-tags" v-model="blog.tags">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-3 col-sm-12">
                            <label class="form-label" for="input-author">作者</label>
                        </div>
                        <div class="col-9 col-sm-12">
                            <input class="form-input" type="text" id="input-author" v-model="blog.author">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-3 col-sm-12">
                            <label class="form-label" for="input-publishtime">发表时间</label>
                        </div>
                        <div class="col-9 col-sm-12">
                            <input class="form-input" type="text" id="input-publishtime" v-model="blog.publishtime">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-3 col-sm-12">
                            <label class="form-label" for="input-source">来源</label>
                        </div>
                        <div class="col-9 col-sm-12">
                            <input class="form-input" type="text" id="input-source" v-model="blog.source">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-3 col-sm-12">
                            <label class="form-label" for="input-smallpic">缩略图</label>
                        </div>
                        <div class="col-9 col-sm-12">
                            <input class="form-input" type="text" id="input-smallpic" v-model="blog.smallpic">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-3 col-sm-12">
                            <label class="form-label" for="input-intro">简介</label>
                        </div>
                        <div class="col-9 col-sm-12">
                            <textarea class="form-input" type="text" id="input-intro" v-model="blog.intro"
                                      rows="4"></textarea>
                        </div>
                    </div>

                    <div style="text-align: right">
                        <input type="button" value="返回" class="btn btn-default"
                               onclick="window.location.href='list'">
                        <input type="button" value="保存" class="btn btn-primary" @click="Save">
                    </div>
                    <div class="form-group">


                        <label class="form-label" for="input-content">博文内容</label>
                        <div id="editor">
                            <!-- Tips: Editor.md can auto append a `<textarea>` tag -->
                            <textarea style="display:none;" id="input-content" v-model="blog.content">### Hello Editor.md !</textarea>
                        </div>
                    </div>


                </form>
            </div>

        </div>

    </div>

    <div class="modal active modal-sm" id="modal-id" v-if="message.length>0">
        <a href="#close" class="modal-overlay" aria-label="Close" @click="Clear()"></a>
        <div class="modal-container">
            <div class="modal-header">
                <a href="#close" class="btn btn-clear float-right" aria-label="Close" @click="Clear()"></a>
                <div class="modal-title h5">消息提示</div>
            </div>
            <div class="modal-body">
                <div class="content">
                    {{message}}
                </div>
            </div>
            <div class="modal-footer">

            </div>
        </div>
    </div>
</div>


<script src="../../static/jquery.min.js"></script>
<script src="../../static/editor.md/editormd.min.js"></script>
<script type="text/javascript">
    $(function () {
        var editor = editormd("editor", {
            // width: "100%",
            height: "600px",
            // markdown: "xxxx",     // dynamic set Markdown text
            path: "./../static/editor.md/lib/"  // Autoload modules mode, codemirror, marked... dependents libs path
        });
    });
</script>


<script>
    // 创建 Vue 实例，得到 ViewModel
    var vm = new Vue({
        el: '#app',
        data() {
            return {
                blog: {
                    blogid: {[{.blogid}]},
                    title: "",
                    urltitle: "",
                    content: "",
                    author: {[{.blogset.Author}]},
                    source: "",
                    publishtime: this.GetDate(),
                    tags: "",
                    intro: "",
                    istop: 0,
                    smallpic: ""
                },
                message: ""
            }
        },
        mounted() {

            this.Load();
        },
        methods: {
            //获取文章
            Load: function () {

                if (this.blog.blogid == 0) {
                    return
                }
                var that = this;

                var date = new Date();
                var sss = date.toLocaleString();
                //发送get请求
                axios.get('../api/v1/blog/' + this.blog.blogid + "?rnd=" + sss).then(function (res) {
                    if (res.data) {
                        if (res.data.status == 200) {
                            that.blog = res.data.data;

                            if (this.blog.istop == 1) {
                                this.blog.istop = true;
                            } else {
                                this.blog.istop = false;
                            }

                        } else {
                            that.message = res.data.message;
                        }
                    }
                }).catch(function (error) {
                    console.log(error);
                });

            },
            //保存
            Save: function () {

                this.errormsg = "";
                var that = this;
                this.blog.content = document.getElementById("input-content").value;
                if (this.blog.title == "") {
                    this.message = "请填写博文标题";
                    return;
                }

                if (this.blog.content == "") {
                    this.message = "请填写博文内容";
                    return;
                }
                try {
                    //替换空白
                    this.blog.urltitle = this.blog.urltitle.replace(new RegExp(/( )/g), "-")
                } catch (e) {

                }

                if (this.blog.istop) {
                    this.blog.istop = 1;
                } else {
                    this.blog.istop = 0;
                }
                //发送get请求
                axios.post('../api/v1/blog', this.blog).then(function (res) {
                    if (res.data) {
                        if (res.data.status == 200) {
                            that.blog.blogid = res.data.data.blogid;
                            that.message = res.data.message;
                        } else {
                            that.message = res.data.message;
                        }
                    }
                }).catch(function (error) {
                    console.log(error);
                });
            },
            Clear: function () {
                this.message = ""
            },
            GetDate: function () {
                var nowtime = new Date();
                var year = nowtime.getFullYear();
                var month = this.Padleft0(nowtime.getMonth() + 1);
                var day = this.Padleft0(nowtime.getDate());
                var hour = this.Padleft0(nowtime.getHours());
                var minute = this.Padleft0(nowtime.getMinutes());
                var second = this.Padleft0(nowtime.getSeconds());
                var millisecond = nowtime.getMilliseconds();
                millisecond = millisecond.toString().length == 1 ? "00" + millisecond : millisecond.toString().length == 2 ? "0" + millisecond : millisecond;
                return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

            },
            Padleft0: function (obj) {
                return obj.toString().replace(/^[0-9]{1}$/, "0" + obj);

            }
        }
    });
</script>

{[{end}]}

</body>
</html>